﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using NetDon.OA.Data;
using NetDon.OA.Data.CollectionClasses;
using NetDon.OA.Data.DaoClasses;
using NetDon.OA.Data.EntityClasses;
using NetDon.OA.Data.HelperClasses;
using NetDon.OA.Data.Linq;
using NetDon.OA.Data.RelationClasses;
using NetDon.OA.Interface;
using NetDon.OA.Interface.eLogistics;
using NetDon.Utilities;
using SD.LLBLGen.Pro.ORMSupportClasses;

namespace NetDon.OA.Business.eLogitics
{
    public class IssuingOrderReceiptInfoTrackService : IIssuingOrderReceiptInfoTrackService
    {
        #region Public
        public int GetOrderCount(IssuingOrderReceiptInfoTrackSearchCriteria criteria)
        {
            var collection = new IssuingOrderReceiptInfoTrackCollection();

            return collection.GetDbCount(GetIssuingOrderReceiptInfoTrackSearchPredicate(criteria), GetIssuingOrderReceiptInfoTrackSearchRelations());
        }

        public System.Data.DataTable SearchOrdersByCondition(IssuingOrderReceiptInfoTrackSearchCriteria criteria, string sortExpression, System.Web.UI.WebControls.SortDirection sortDirection, int pageSize, int pageIndex)
        {
            var fields = new EntityFields(26);
            var index = 0;
            fields[index++] = IssuingOrderReceiptInfoTrackFields.Id;
            fields[index++] = IssuingOrderReceiptInfoTrackFields.IssuingOrderId;
            fields[index++] = IssuingOrderReceiptInfoTrackFields.IssuingOrderDeliveryId;
            fields[index++] = IssuingOrderReceiptInfoTrackFields.ReceiptInfoCopies;
            fields[index++] = IssuingOrderReceiptInfoTrackFields.ReceiptOnWayDays;
            fields[index++] = IssuingOrderReceiptInfoTrackFields.ReceiptInfoPlannedResponseDate;
            fields[index++] = IssuingOrderReceiptInfoTrackFields.ReceiptInfoActualResponseDate;
            fields[index++] = IssuingOrderReceiptInfoTrackFields.ReceiptInfoNotReceived;
            fields[index++] = IssuingOrderReceiptInfoTrackFields.ReceiptInfoDelayedDays;
            fields[index++] = IssuingOrderReceiptInfoTrackFields.IsReceiptInfoDelayed;
            fields[index++] = IssuingOrderReceiptInfoTrackFields.IsReceiptInfoExceptional;
            fields[index++] = IssuingOrderReceiptInfoTrackFields.ReceiptOnWayDaysUnit;
            fields[index++] = IssuingOrderReceiptInfoTrackFields.LastUpdatedBy;
            fields[index++] = IssuingOrderReceiptInfoTrackFields.LastUpdatedDate;

            fields[index++] = IssuingOrderDeliveryFields.DeliveryOrderCode;
            fields[index++] = IssuingOrderDeliveryFields.DeliveryCity;

            fields[index++] = IssuingOrderFields.BusinessType;
            fields[index++] = IssuingOrderFields.TransportationPlanCode;
            fields[index++] = IssuingOrderFields.OrderCode;
            fields[index++] = IssuingOrderFields.GoodsShippedTypeId;
            fields[index++] = IssuingOrderFields.ReceivingOrderDate;
            fields[index++] = IssuingOrderFields.OrderStatus;
            fields[index++] = IssuingOrderFields.OrderQuantity;

            fields[index++] = CustomerInfoFields.CustomerName;
            fields[index++] = IssuingOrderFields.RouteTypeId;
            fields[index++] = UserFields.UserName;


            EntityField sortField = null;
            switch (sortExpression)
            {
                case "BusinessType":
                    sortField = IssuingOrderFields.BusinessType;
                    break;
                case "TransportationPlanCode":
                    sortField = IssuingOrderFields.TransportationPlanCode;
                    break;
                case "OrderCode":
                    sortField = IssuingOrderFields.OrderCode;
                    break;
                case "GoodsShippedTypeId":
                    sortField = IssuingOrderFields.GoodsShippedTypeId;
                    break;
                case "ReceivingOrderDate":
                    sortField = IssuingOrderFields.ReceivingOrderDate;
                    break;
                case "OrderStatus":
                    sortField = IssuingOrderFields.OrderStatus;
                    break;
                case "OrderQuantity":
                    sortField = IssuingOrderFields.OrderQuantity;
                    break;
                case "CustomerName":
                    sortField = CustomerInfoFields.CustomerName;
                    break;
                case "RouteTypeId":
                    sortField = IssuingOrderFields.RouteTypeId;
                    break;

                case "DeliveryOrderCode":
                    sortField = IssuingOrderDeliveryFields.DeliveryOrderCode;
                    break;

                case "IssuingOrderId":
                    sortField = IssuingOrderReceiptInfoTrackFields.IssuingOrderId;
                    break;
                case "IssuingOrderDeliveryId":
                    sortField = IssuingOrderReceiptInfoTrackFields.IssuingOrderDeliveryId;
                    break;
                case "ReceiptInfoCopies":
                    sortField = IssuingOrderReceiptInfoTrackFields.ReceiptInfoCopies;
                    break;
                case "ReceiptInfoOnWayDays":
                    sortField = IssuingOrderReceiptInfoTrackFields.ReceiptOnWayDays;
                    break;
                case "ReceiptInfoPlannedResponseDate":
                    sortField = IssuingOrderReceiptInfoTrackFields.ReceiptInfoPlannedResponseDate;
                    break;
                case "ReceiptInfoActualResponseDate":
                    sortField = IssuingOrderReceiptInfoTrackFields.ReceiptInfoActualResponseDate;
                    break;
                case "ReceiptInfoNotReceived":
                    sortField = IssuingOrderReceiptInfoTrackFields.ReceiptInfoNotReceived;
                    break;
                case "ReceiptInfoDelayedDays":
                    sortField = IssuingOrderReceiptInfoTrackFields.ReceiptInfoDelayedDays;
                    break;
                case "IsReceiptInfoDelayed":
                    sortField = IssuingOrderReceiptInfoTrackFields.IsReceiptInfoDelayed;
                    break;
                case "IsReceiptInfoExceptional":
                    sortField = IssuingOrderReceiptInfoTrackFields.IsReceiptInfoExceptional;
                    break;
                case "ReceiptOnWayDaysUnit":
                    sortField = IssuingOrderReceiptInfoTrackFields.ReceiptOnWayDaysUnit;
                    break;


            }
            SortExpression expression;
            if ((object)sortField != null)
            {
                SortOperator sortOperator = sortDirection == SortDirection.Ascending
                                                ? SortOperator.Ascending
                                                : SortOperator.Descending;
                expression = new SortExpression(sortField | sortOperator);
            }
            else
            {

                expression = new SortExpression(IssuingOrderFields.TransportationPlanCode | SortOperator.Descending);
                expression.Add(IssuingOrderDeliveryFields.DeliveryOrderCode | SortOperator.Descending);
                expression.Add(IssuingOrderReceiptInfoTrackFields.ReceiptInfoPlannedResponseDate | SortOperator.Descending);
            }


            var table = new DataTable();
            var dao = new TypedListDAO();
            dao.GetMultiAsDataTable(fields, table, 0, expression, GetIssuingOrderReceiptInfoTrackSearchPredicate(criteria),
                                    GetIssuingOrderReceiptInfoTrackSearchRelations(), false, null, null, pageIndex + 1, pageSize);


            return table;
        }

        public bool AddIssuingOrderReceiptInfoTrack(IssuingOrderReceiptInfoTrackData issuingOrderData)
        {
            var trans = new Transaction(IsolationLevel.ReadCommitted, "AddIssuingOrderReceiptInfoTrack");
            try
            {
                // Save Order
                AddIssuingOrderReceiptInfoTrack(issuingOrderData, trans);

                trans.Commit();
                return true;
            }
            catch (Exception)
            {
                trans.Rollback();
                throw;
            }
        }

        public bool ModifyIssuingOrderReceiptInfoTrack(IssuingOrderReceiptInfoTrackData issuingOrderData)
        {
            var trans = new Transaction(IsolationLevel.ReadCommitted, "ModifyIssuingOrderReceiptInfoTrack");
            try
            {
                // Save Order
                ModifyIssuingOrderReceiptInfoTrack(issuingOrderData, trans);
                trans.Commit();
                return true;
            }
            catch (Exception)
            {
                trans.Rollback();
                throw;
            }
        }

        public bool RemoveIssuingOrderReceiptInfoTrack(Guid issuingOrderReceiptInfoTrackId)
        {
            var trans = new Transaction(IsolationLevel.ReadCommitted, "RemoveIssuingOrderReceiptInfoTrack");
            try
            {

                var orderItemsCollection = new IssuingOrderReceiptInfoTrackCommentCollection();
                trans.Add(orderItemsCollection);
                orderItemsCollection.DeleteMulti(IssuingOrderOnWayTrackCommentFields.IssuingOrderOnWayTrackId == issuingOrderReceiptInfoTrackId);

                var orderCollection = new IssuingOrderReceiptInfoTrackCollection();
                trans.Add(orderCollection);
                orderCollection.DeleteMulti(IssuingOrderOnWayTrackFields.Id == issuingOrderReceiptInfoTrackId);

                trans.Commit();
                return true;
            }
            catch (Exception)
            {
                trans.Rollback();
                throw;
            }
        }

        public IssuingOrderReceiptInfoTrackData GetIssuingOrderReceiptInfoTrack(Guid issuingOrderReceiptInfoTrackId)
        {
            return GetIssuingOrderReceiptInfoTrackData(new IssuingOrderReceiptInfoTrackEntity(issuingOrderReceiptInfoTrackId));
        }

        public bool RemoveIssuingOrderReceiptInfoTrackComment(Guid issuingOrderReceiptInfoTrackCommentId)
        {
            var trans = new Transaction(IsolationLevel.ReadCommitted, "RemoveIssuingOrderReceiptInfoTrackComment");
            try
            {

                var issuingOrderOnWayTrackCommentEntity = new IssuingOrderReceiptInfoTrackCommentEntity(issuingOrderReceiptInfoTrackCommentId);
                trans.Add(issuingOrderOnWayTrackCommentEntity);
                issuingOrderOnWayTrackCommentEntity.Delete();

                trans.Commit();
                return true;
            }
            catch (Exception)
            {
                trans.Rollback();
                throw;
            }
        }

        public IssuingOrderReceiptInfoTrackCommentData GetIssuingOrderReceiptInfoTrackComment(Guid issuingOrderReceiptInfoTrackCommentId)
        {
            var issuingOrderDeliveryEntity = new IssuingOrderReceiptInfoTrackCommentEntity(issuingOrderReceiptInfoTrackCommentId);
            return GetIssuingOrderReceiptInfoTrackCommentData(issuingOrderDeliveryEntity);
        }

        public IEnumerable<IssuingOrderReceiptInfoTrackCommentData> GetIssuingOrderReceiptInfoTrackCommentById(Guid issuingOrderReceiptInfoTrackId)
        {
            var issuingOrderDeliveries = new IssuingOrderReceiptInfoTrackCommentCollection();
            issuingOrderDeliveries.GetMulti(IssuingOrderReceiptInfoTrackCommentFields.IssuingOrderReceiptInfoTrackId == issuingOrderReceiptInfoTrackId);
            return from issuingOrderDelivery in issuingOrderDeliveries select GetIssuingOrderReceiptInfoTrackCommentData(issuingOrderDelivery);
        }

       public bool CheckIssuingOrderReceiptInfoExists(Guid issuingOrderId)
        {
            var metaData = new LinqMetaData();
            return 
                 metaData.IssuingOrderReceiptInfoTrack.Any(
                     order => order.IssuingOrderId == issuingOrderId);
            
        }

        #endregion

        #region Private provider
        private IssuingOrderReceiptInfoTrackCommentEntity GetIssuingOrderReceiptInfoTrackCommentEntity(IssuingOrderReceiptInfoTrackCommentData issuingOrderOnWayTrackCommentData, IssuingOrderReceiptInfoTrackCommentEntity issuingOrderOnWayTrackCommentEntity)
        {

            issuingOrderOnWayTrackCommentEntity.ReceiptInfoExceptions = issuingOrderOnWayTrackCommentData.ReceiptInfoExceptions;
            //issuingOrderOnWayTrackCommentEntity.ReceiptInfoDelayedReason = issuingOrderOnWayTrackCommentData.ReceiptInfoDelayedReason;
            issuingOrderOnWayTrackCommentEntity.Remark = issuingOrderOnWayTrackCommentData.Remark;

            return issuingOrderOnWayTrackCommentEntity;
        }
        private IssuingOrderReceiptInfoTrackCommentData GetIssuingOrderReceiptInfoTrackCommentData(IssuingOrderReceiptInfoTrackCommentEntity issuingOrderOnWayTrackCommentEntity)
        {
            return new IssuingOrderReceiptInfoTrackCommentData()
            {
                Id = issuingOrderOnWayTrackCommentEntity.Id,
                //ReceiptInfoDelayedReason = issuingOrderOnWayTrackCommentEntity.ReceiptInfoDelayedReason,
                ReceiptInfoExceptions = issuingOrderOnWayTrackCommentEntity.ReceiptInfoExceptions,
                IssuingOrderReceiptInfoTrackId = issuingOrderOnWayTrackCommentEntity.IssuingOrderReceiptInfoTrackId,
                Remark = issuingOrderOnWayTrackCommentEntity.Remark

            };
        }
        private IssuingOrderReceiptInfoTrackData GetIssuingOrderReceiptInfoTrackData(IssuingOrderReceiptInfoTrackEntity issuingOrderEntity)
        {
            var issuingOrderData = new IssuingOrderReceiptInfoTrackData()
            {
                Id = issuingOrderEntity.Id,
                ReceiptInfoActualResponseDate = issuingOrderEntity.ReceiptInfoActualResponseDate,
                AreaId = issuingOrderEntity.AreaId,
                ReceiptInfoCopies = issuingOrderEntity.ReceiptInfoCopies,
                ReceiptInfoDelayedDays = issuingOrderEntity.ReceiptInfoDelayedDays,
                ReceiptInfoNotReceived = issuingOrderEntity.ReceiptInfoNotReceived,
                ReceiptOnWayDays = issuingOrderEntity.ReceiptOnWayDays,
                ReceiptInfoPlannedResponseDate = issuingOrderEntity.ReceiptInfoPlannedResponseDate,
                IsReceiptInfoExceptional = issuingOrderEntity.IsReceiptInfoExceptional,
                IsReceiptInfoDelayed = issuingOrderEntity.IsReceiptInfoDelayed,
                CreateBy = issuingOrderEntity.CreateBy,
                CreatedDate = issuingOrderEntity.CreatedDate,
                LastUpdatedBy = issuingOrderEntity.LastUpdatedBy,
                LastUpdatedDate = issuingOrderEntity.LastUpdatedDate,
                IssuingOrderDeliveryId = issuingOrderEntity.IssuingOrderDeliveryId,
                IssuingOrderId = issuingOrderEntity.IssuingOrderId,
                ReceiptOnWayDaysUnit = issuingOrderEntity.ReceiptOnWayDaysUnit


            };
      

            return issuingOrderData;
        }
        private IssuingOrderReceiptInfoTrackEntity GetIssuingOrderReceiptInfoTrackEntity(IssuingOrderReceiptInfoTrackEntity issuingOrderReceiptInfoTrackEntity, IssuingOrderReceiptInfoTrackData issuingOrderReceiptInfoTrackData)
        {

            issuingOrderReceiptInfoTrackEntity.ReceiptInfoActualResponseDate = issuingOrderReceiptInfoTrackData.ReceiptInfoActualResponseDate;
            issuingOrderReceiptInfoTrackEntity.AreaId = issuingOrderReceiptInfoTrackData.AreaId;
            issuingOrderReceiptInfoTrackEntity.ReceiptInfoCopies = issuingOrderReceiptInfoTrackData.ReceiptInfoCopies;
            issuingOrderReceiptInfoTrackEntity.ReceiptInfoDelayedDays = issuingOrderReceiptInfoTrackData.ReceiptInfoDelayedDays;
            issuingOrderReceiptInfoTrackEntity.ReceiptInfoNotReceived = issuingOrderReceiptInfoTrackData.ReceiptInfoNotReceived;
            issuingOrderReceiptInfoTrackEntity.ReceiptOnWayDays = issuingOrderReceiptInfoTrackData.ReceiptOnWayDays;
            issuingOrderReceiptInfoTrackEntity.ReceiptInfoPlannedResponseDate = issuingOrderReceiptInfoTrackData.ReceiptInfoPlannedResponseDate;
            issuingOrderReceiptInfoTrackEntity.IsReceiptInfoDelayed = issuingOrderReceiptInfoTrackData.IsReceiptInfoDelayed;
            issuingOrderReceiptInfoTrackEntity.IsReceiptInfoExceptional = issuingOrderReceiptInfoTrackData.IsReceiptInfoExceptional;
            issuingOrderReceiptInfoTrackEntity.IssuingOrderId = issuingOrderReceiptInfoTrackData.IssuingOrderId;
            issuingOrderReceiptInfoTrackEntity.IssuingOrderDeliveryId = issuingOrderReceiptInfoTrackData.IssuingOrderDeliveryId;
            issuingOrderReceiptInfoTrackEntity.ReceiptOnWayDaysUnit =
                issuingOrderReceiptInfoTrackData.ReceiptOnWayDaysUnit;

            issuingOrderReceiptInfoTrackEntity.CreateBy = issuingOrderReceiptInfoTrackData.CreateBy;
            issuingOrderReceiptInfoTrackEntity.CreatedDate = issuingOrderReceiptInfoTrackData.CreatedDate;
            issuingOrderReceiptInfoTrackEntity.LastUpdatedBy = issuingOrderReceiptInfoTrackData.LastUpdatedBy;
            issuingOrderReceiptInfoTrackEntity.LastUpdatedDate = issuingOrderReceiptInfoTrackData.LastUpdatedDate;

            return issuingOrderReceiptInfoTrackEntity;
        }
        private IPredicate GetIssuingOrderReceiptInfoTrackSearchPredicate(IssuingOrderReceiptInfoTrackSearchCriteria criteria)
        {
            var expression = new PredicateExpression();

            if (criteria.Id.HasValue && criteria.Id.Value != Guid.Empty)
            {
                expression.Add(IssuingOrderFields.Id == criteria.Id);
            }
            if (criteria.AreaId.HasValue && criteria.AreaId.Value != Guid.Empty)
            {
                expression.Add(IssuingOrderReceiptInfoTrackFields.AreaId == criteria.AreaId);
            }
            if (criteria.OrderStatus.HasValue)
            {
                expression.Add(IssuingOrderFields.OrderStatus == criteria.OrderStatus);
            }
            if (criteria.BusinessType.HasValue)
            {
                expression.Add(IssuingOrderFields.BusinessType == criteria.BusinessType);
            }

            if (!string.IsNullOrEmpty(criteria.OrderCode))
            {
                expression.Add(IssuingOrderFields.OrderCode % ("%" + criteria.OrderCode + "%"));
            }
            if (criteria.CustomerId.HasValue && criteria.CustomerId.Value != Guid.Empty)
            {
                expression.Add(IssuingOrderFields.CustomerId == criteria.CustomerId);
            }

            if (!string.IsNullOrEmpty(criteria.CustomerName))
            {
                expression.Add(CustomerInfoFields.CustomerName % ("%" + criteria.CustomerName + "%"));
            }

            if (!string.IsNullOrEmpty(criteria.CustomerCode))
            {
                expression.Add(CustomerInfoFields.CustomerCode % ("%" + criteria.CustomerCode + "%"));
            }
            if (criteria.RouteTypeId.HasValue && criteria.RouteTypeId.Value != Guid.Empty)
            {
                expression.Add(IssuingOrderFields.RouteTypeId == criteria.RouteTypeId);
            }

            if (!string.IsNullOrEmpty(criteria.TransportationPlanCode))
            {
                expression.Add(IssuingOrderFields.TransportationPlanCode % ("%" + criteria.TransportationPlanCode + "%"));
            }
            if (criteria.CheckoutType.HasValue)
            {
                expression.Add(IssuingOrderFields.CheckoutType == criteria.CheckoutType);
            }
            if (criteria.IsReceiptInfoDelayed.HasValue)
            {
                expression.Add(IssuingOrderReceiptInfoTrackFields.IsReceiptInfoDelayed == criteria.IsReceiptInfoDelayed);
            }
            if (criteria.IsReceiptInfoExceptional.HasValue)
            {
                expression.Add(IssuingOrderReceiptInfoTrackFields.IsReceiptInfoExceptional == criteria.IsReceiptInfoExceptional);
            }

            if (criteria.ReceivingOrderDate != null)
            {
                if (criteria.ReceivingOrderDate.Start.HasValue)
                {
                    expression.Add(IssuingOrderFields.ReceivingOrderDate >= criteria.ReceivingOrderDate.Start);
                }
                if (criteria.ReceivingOrderDate.End.HasValue)
                {
                    expression.Add(IssuingOrderFields.ReceivingOrderDate <= criteria.ReceivingOrderDate.End);
                }
            }


            if (criteria.ReceiptInfoPlannedResponseDate != null)
            {
                if (criteria.ReceiptInfoPlannedResponseDate.Start.HasValue)
                {
                    expression.Add(IssuingOrderReceiptInfoTrackFields.ReceiptInfoPlannedResponseDate >= criteria.ReceiptInfoPlannedResponseDate.Start);
                }
                if (criteria.ReceiptInfoPlannedResponseDate.End.HasValue)
                {
                    expression.Add(IssuingOrderReceiptInfoTrackFields.ReceiptInfoPlannedResponseDate <= criteria.ReceiptInfoPlannedResponseDate.End);
                }
            }

            if (criteria.ReceiptInfoActualResponseDate != null)
            {
                if (criteria.ReceiptInfoActualResponseDate.Start.HasValue)
                {
                    expression.Add(IssuingOrderReceiptInfoTrackFields.ReceiptInfoActualResponseDate >= criteria.ReceiptInfoActualResponseDate.Start);
                }
                if (criteria.ReceiptInfoActualResponseDate.End.HasValue)
                {
                    expression.Add(IssuingOrderReceiptInfoTrackFields.ReceiptInfoActualResponseDate <= criteria.ReceiptInfoActualResponseDate.End);
                }
            }
            if (!string.IsNullOrWhiteSpace(criteria.DeliveryProvince))
            {
                expression.Add(IssuingOrderFields.DeliveryProvince == criteria.DeliveryProvince);
            }
            if (!string.IsNullOrWhiteSpace(criteria.DeliveryCity))
            {
                expression.Add(IssuingOrderDeliveryFields.DeliveryCity == criteria.DeliveryCity);

            }
            if (!string.IsNullOrWhiteSpace(criteria.OrderDeliveryCode))
            {
                expression.Add(IssuingOrderDeliveryFields.DeliveryOrderCode % ("%" + criteria.OrderDeliveryCode + "%"));

            }

            return expression;
        }
        private IRelationCollection GetIssuingOrderReceiptInfoTrackSearchRelations()
        {
            var relation = new RelationCollection(IssuingOrderReceiptInfoTrackEntity.Relations.IssuingOrderEntityUsingIssuingOrderId, JoinHint.Left);
            relation.Add(IssuingOrderReceiptInfoTrackEntity.Relations.IssuingOrderDeliveryEntityUsingIssuingOrderDeliveryId, JoinHint.Left);
            relation.Add(IssuingOrderEntity.Relations.CustomerInfoEntityUsingCustomerId, JoinHint.Left);
            relation.Add(new DynamicRelation(EntityType.IssuingOrderReceiptInfoTrackEntity, JoinHint.Left, EntityType.UserEntity, string.Empty, string.Empty,
                                                IssuingOrderReceiptInfoTrackFields.LastUpdatedBy == UserFields.UserId));
            return relation;
        }
        private void AddIssuingOrderReceiptInfoTrack(IssuingOrderReceiptInfoTrackData issuingOrderOnWayTrackData, Transaction trans)
        {
            var issuingOrderReceiptInfoTrackEntit = new IssuingOrderReceiptInfoTrackEntity();
            GetIssuingOrderReceiptInfoTrackEntity(issuingOrderReceiptInfoTrackEntit, issuingOrderOnWayTrackData);
            issuingOrderReceiptInfoTrackEntit.Id = issuingOrderOnWayTrackData.Id;

            trans.Add(issuingOrderReceiptInfoTrackEntit);
            issuingOrderReceiptInfoTrackEntit.Save();

            AddIssuingOrderReceiptInfoTrackComment(issuingOrderOnWayTrackData, trans, issuingOrderReceiptInfoTrackEntit);
        }
        private void AddIssuingOrderReceiptInfoTrackComment(IssuingOrderReceiptInfoTrackData issuingOrderOnWayTrackData, Transaction trans, IssuingOrderReceiptInfoTrackEntity issuingOrderOnWayTrackEntity)
        {
            if (issuingOrderOnWayTrackData.IssuingOrderReceiptInfoTrackCommentList == null) return;
            foreach (var issuingOrderOnWayTrackComment in issuingOrderOnWayTrackData.IssuingOrderReceiptInfoTrackCommentList)
            {

                IssuingOrderReceiptInfoTrackCommentEntity issuingOrderReceiptInfoTrackCommentEntity =
                    new IssuingOrderReceiptInfoTrackCommentEntity(issuingOrderOnWayTrackComment.Id);
                bool isNotDataValid = string.IsNullOrWhiteSpace(issuingOrderOnWayTrackComment.ReceiptInfoExceptions);
                if (issuingOrderReceiptInfoTrackCommentEntity.IsNew)
                {
                    if (isNotDataValid)
                    {
                        continue;
                    }
                    issuingOrderReceiptInfoTrackCommentEntity.Id = Guid.NewGuid();
                }
                else
                {
                    if (isNotDataValid)
                    {
                        issuingOrderReceiptInfoTrackCommentEntity.Delete();
                        trans.Add(issuingOrderReceiptInfoTrackCommentEntity);
                        issuingOrderReceiptInfoTrackCommentEntity.Save();
                        continue;
                    }
                }
                GetIssuingOrderReceiptInfoTrackCommentEntity(issuingOrderOnWayTrackComment, issuingOrderReceiptInfoTrackCommentEntity);

                issuingOrderReceiptInfoTrackCommentEntity.IssuingOrderReceiptInfoTrackId = issuingOrderOnWayTrackEntity.Id;
                trans.Add(issuingOrderReceiptInfoTrackCommentEntity);
                issuingOrderReceiptInfoTrackCommentEntity.Save();
            }
        }
        private void ModifyIssuingOrderReceiptInfoTrack(IssuingOrderReceiptInfoTrackData issuingOrderReceiptInfoTrackData, Transaction trans)
        {
            var issuingOrderEntity = new IssuingOrderEntity(issuingOrderReceiptInfoTrackData.IssuingOrderId);
            var isExisting =
                issuingOrderEntity.IssuingOrderReceiptInfoTrack.Any(
                    r =>
                    (!r.ReceiptInfoActualResponseDate.HasValue ||
                    r.ReceiptInfoActualResponseDate.Value <= DateTime.MinValue) && r.Id != issuingOrderReceiptInfoTrackData.Id);

            var issuingOrderReceiptInfoEntity = new IssuingOrderReceiptInfoTrackEntity(issuingOrderReceiptInfoTrackData.Id);
            GetIssuingOrderReceiptInfoTrackEntity(issuingOrderReceiptInfoEntity, issuingOrderReceiptInfoTrackData);
            trans.Add(issuingOrderReceiptInfoEntity);
            issuingOrderReceiptInfoEntity.Save();
            if (issuingOrderReceiptInfoTrackData.ReceiptInfoActualResponseDate.HasValue && issuingOrderReceiptInfoTrackData.ReceiptInfoActualResponseDate.Value > DateTime.MinValue)
            {
                if (!isExisting)
                {
                    issuingOrderEntity.OrderStatus = issuingOrderEntity.OrderStatus > (int)OrderStatus.ReceiptTrackComplted ? issuingOrderEntity.OrderStatus : (int)OrderStatus.ReceiptTrackComplted;

                    trans.Add(issuingOrderEntity);
                    issuingOrderEntity.Save();
                }

            }
            AddIssuingOrderReceiptInfoTrackComment(issuingOrderReceiptInfoTrackData, trans, issuingOrderReceiptInfoEntity);
        }


        #endregion
    }
}

